////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  PlayerManagerViewController.m
//  PokerHandUp
//
//  Created by psvn047 on 6/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#import "PlayerManagerViewController.h"
#import "PlayerViewController.h"
#import "AppDelegate.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@interface PlayerManagerViewController ()
@property(nonatomic, retain) PlayerViewController * myPlayerViewController;
@property(nonatomic, retain) PlayerViewController * rivalPlayerViewController;
@property(nonatomic, retain) UILabel * lblDealer;
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation PlayerManagerViewController
@synthesize rivalPlayer = _rivalPlayer;
@synthesize myPlayer = _myPlayer;
@synthesize myPlayerViewController = _myPlayerViewController;
@synthesize rivalPlayerViewController = _rivalPlayerViewController;
@synthesize playerManagerCallback =_playerManagerCallback;
@synthesize lblDealer = _lblDealer;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CGRect firstCooRivalPlayer;
CGRect firstCooMyPlayer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
      
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    firstCooRivalPlayer = [self.rivalPlayer frame];
    firstCooMyPlayer = [self.myPlayer frame];
    [self.rivalPlayer removeFromSuperview];
    [self.myPlayer removeFromSuperview];
    
    self.myPlayerViewController = [[PlayerViewController new] autorelease];
    self.rivalPlayerViewController = [[PlayerViewController new] autorelease];
    self.rivalPlayer =[self.rivalPlayerViewController view];
    self.myPlayer = [self.myPlayerViewController view];
        
    [self.rivalPlayer setFrame:firstCooRivalPlayer];
    [self.myPlayer setFrame:firstCooMyPlayer];
    
    [self.view addSubview:self.rivalPlayer];
    [self.view addSubview:self.myPlayer];
    
    self.lblDealer = [[[UILabel alloc]initWithFrame:CGRectMake(74, 20, 100, 20)] autorelease];
    self.lblDealer.textAlignment = UITextAlignmentLeft;
    self.lblDealer.backgroundColor = [UIColor clearColor];
    self.lblDealer.textColor = [UIColor redColor];
    self.lblDealer.text = @"Dealer";
    [self.view addSubview:self.lblDealer];
    [self.lblDealer setHidden:YES];
    
  }
-(void) controlRivalPlayerAni:(NSString*) value
{
    UILabel *labelOne = [[[UILabel alloc]initWithFrame:CGRectMake(200, 90, 100, 20)] autorelease];
    labelOne.textAlignment = UITextAlignmentCenter;
    labelOne.backgroundColor = [UIColor clearColor];
    labelOne.textColor = [UIColor orangeColor];
    labelOne.text = value;
    
    [self.view addSubview:labelOne];
    [UIView animateWithDuration:1 delay:0.0f options:UIViewAnimationCurveEaseIn
                     animations:^{
                         [labelOne setFrame:CGRectMake(200, 140, 100, 20)];
                        // labelOne.alpha = 0;
                     }
                     completion:^(BOOL finished) {
                         [labelOne removeFromSuperview];
                     }
     ];
    
}

-(void) subRivalPlayerAni:(long) money
{
    [self.rivalPlayerViewController subMoney:money];
    UILabel *labelOne = [[[UILabel alloc]initWithFrame:CGRectMake(200, 90, 100, 20)] autorelease];
    labelOne.textAlignment = UITextAlignmentCenter;
    labelOne.backgroundColor = [UIColor clearColor];
    labelOne.textColor = [UIColor orangeColor];
    labelOne.text = [[NSString alloc]initWithFormat:@"-$%d",money];
    
    [self.view addSubview:labelOne];
    [UIView animateWithDuration:0.5 delay:0.0f options:UIViewAnimationCurveEaseIn
                     animations:^{
                         [labelOne setFrame:CGRectMake(200, 140, 100, 20)];
                     }
                     completion:^(BOOL finished) {
                         [labelOne removeFromSuperview];
                     }
     ];
    
}

-(void) addRivalPlayerAni:(long) money
{
    [self.rivalPlayerViewController addMoney:money];
    UILabel *labelOne = [[[UILabel alloc]initWithFrame:CGRectMake(200, 140, 100, 20)] autorelease];
    labelOne.textAlignment = UITextAlignmentCenter;
    labelOne.backgroundColor = [UIColor clearColor];
    labelOne.textColor = [UIColor orangeColor];
    labelOne.text = [[NSString alloc]initWithFormat:@"+$%d",money];
    
    [self.view addSubview:labelOne];
    [UIView animateWithDuration:0.5 delay:0.0f options:UIViewAnimationCurveEaseIn
                     animations:^{
                         [labelOne setFrame:CGRectMake(200, 90, 100, 20)];
                     }
                     completion:^(BOOL finished) {
                         [labelOne removeFromSuperview];
                     }
     ];
    
}

-(void) subMyPlayerAni:(long) money
{
    [self.myPlayerViewController subMoney:money];
    UILabel *labelOne = [[[UILabel alloc]initWithFrame:CGRectMake(200, 347, 100, 20)] autorelease];
    labelOne.textAlignment = UITextAlignmentCenter;
    labelOne.backgroundColor = [UIColor clearColor];
    labelOne.textColor = [UIColor orangeColor];
    labelOne.text = [[NSString alloc]initWithFormat:@"-$%d",money];
    
    [self.view addSubview:labelOne];
    [UIView animateWithDuration:0.5 delay:0.0f options:UIViewAnimationCurveEaseIn
                     animations:^{
                         [labelOne setFrame:CGRectMake(200, 300, 100, 20)];
                     }
                     completion:^(BOOL finished) {
                         [labelOne removeFromSuperview];
                     }
     ];
    
}

-(void) addMyPlayerAni:(long) money
{
    [self.myPlayerViewController addMoney:money];
    UILabel *labelOne = [[[UILabel alloc]initWithFrame:CGRectMake(200, 300, 100, 20)] autorelease];
    labelOne.textAlignment = UITextAlignmentCenter;
    labelOne.backgroundColor = [UIColor clearColor];
    labelOne.textColor = [UIColor orangeColor];
    labelOne.text = [[NSString alloc]initWithFormat:@"+$%d",money];
    
    [self.view addSubview:labelOne];
    [UIView animateWithDuration:0.5 delay:0.0f options:UIViewAnimationCurveEaseIn
                     animations:^{
                         [labelOne setFrame:CGRectMake(200, 347, 100, 20)];
                     }
                     completion:^(BOOL finished) {
                         [labelOne removeFromSuperview];
                     }
     ];

}

-(void) updateMyDealer
{
    [self.lblDealer setHidden:NO];
    [self.lblDealer setFrame:CGRectMake(74, 330, 100, 20)];
}
-(void) updateRivalDealer
{
    [self.lblDealer setHidden:NO];
    [self.lblDealer setFrame:CGRectMake(74, 20, 100, 20)];
}

-(void) updateInfo:(NSMutableDictionary*) data
{
    PlayerInfo* player = (PlayerInfo *)[data objectForKey:@"rivalPlayer"];
    [self.rivalPlayerViewController updateInfo:player];
    player = (PlayerInfo *)[data objectForKey:@"myPlayer"];
    [self.myPlayerViewController updateInfo:player];
    
    [self playerAnimation];
}
-(void) playerAnimation
{
    [self.rivalPlayer setFrame:firstCooRivalPlayer];
    [self.myPlayer setFrame:firstCooMyPlayer];
    
    [UIView animateWithDuration:1.0f
                     animations:^{
                         [self.rivalPlayer setFrame:CGRectMake(74, 37, self.rivalPlayer.frame.size.width, self.rivalPlayer.frame.size.height)];
                     }
                     completion:^(BOOL finished) {
                         
                     }
     ];
    
    [UIView animateWithDuration:1.0f
                     animations:^{
                         [self.myPlayer setFrame:CGRectMake(74, 347, self.myPlayer.frame.size.width, self.myPlayer.frame.size.height)];
                     }
                     completion:^(BOOL finished) {
                         [self.playerManagerCallback playerAnimationComplete];
                     }
     ];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [self.myPlayer release];
    [self.rivalPlayer release];
    self.myPlayerViewController = nil;
    self.rivalPlayerViewController = nil;
    [self.playerManagerCallback release];
    [self.lblDealer release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////